#!/usr/bin/env python
# coding: utf-8

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

import module.grouppurchase
import logging
import os
import data
    
class MainHandler(webapp.RequestHandler):    
    def get(self):
        itemkey = self.request.get('itemkey')
        action = self.request.get('action')
        userid = self.request.get('userid')
        start = self.request.get('start')           # not required, default 0
        limit = self.request.get('limit')           # not required, default 10
        result = None
        
        self.response.headers['Content-Type'] = 'text/xml'
        
        if action == "getlist":
            if start == "":
                start=0
            if limit == "":
                limit=100
            gps = module.grouppurchase.GroupPurchase.get_gp_list(start, limit)
            path = os.path.join(os.path.dirname(__file__), '../templetes/gp_list.xml')
            self.response.out.write(template.render(path, 
                                                {"gps": gps}))
            return
        elif action == "getproduct":
            product = module.grouppurchase.GroupPurchase.get_product(itemkey)
            
            path = os.path.join(os.path.dirname(__file__), '../templetes/product_key_result.xml')
            self.response.out.write(template.render(path, 
                                                { 
                                                 "itemkey": itemkey, 
                                                 "item": product}))
            
            return     
        elif action == "getentry":
            group_purchase_item = module.grouppurchase.GroupPurchase.get_entry(itemkey)
            duser_count = len(group_purchase_item.deposit_userid_list)
            nuser_count = len(group_purchase_item.nondeposit_userid_list)
            
            logging.info("duser_count: " + str(duser_count))
            
            path = os.path.join(os.path.dirname(__file__), '../templetes/gp_item.xml')
            self.response.out.write(template.render(path, 
                                                {"group_purchase_item": group_purchase_item, 
                                                 "duser_count": duser_count, 
                                                 "itemkey": itemkey, 
                                                 "nuser_count": nuser_count}))
            return
        elif action == "makenew":
            title = self.request.get('title')
            trigger_userid = self.request.get('trigger_userid')
            description = self.request.get('description')
            photo_url = self.request.get('photo_url')
            try:
                lprice = int(self.request.get('lprice'))
            except:
                lprice = -1;
            
            gpkey = module.grouppurchase.GroupPurchase.add_new_entry(title, 
                                                               trigger_userid, 
                                                               description,
                                                               photo_url,
                                                               lprice)
            
            path = os.path.join(os.path.dirname(__file__), '../templetes/gp_request_result.xml')
            self.response.out.write(template.render(path, 
                                                {"title": title, 
                                                 "trigger_userid": trigger_userid, 
                                                 "description": description, 
                                                 "photo_url": photo_url, 
                                                 "lprice": lprice, 
                                                 "gpkey": gpkey}))
            return
        elif action == "check":
            gps = module.grouppurchase.GroupPurchase.check_launching(userid)
            path = os.path.join(os.path.dirname(__file__), '../templetes/gp_check_list.xml')
            self.response.out.write(template.render(path, 
                                                {"userid": userid, 
                                                 "gps": gps}))
            return 
        elif action == "updatecount":
            count = self.request.get('count')
            if count == "":
                count = 0;
            else:
                count = int(count)
            result = module.grouppurchase.GroupPurchase.update_nondeposit_count(itemkey, userid, count)
        elif action == "launch":
            result = module.grouppurchase.GroupPurchase.launch_gp(itemkey, userid)
        elif action == "djoin":
            result = module.grouppurchase.GroupPurchase.deposit_join(itemkey, userid)
        elif action == "njoin":
            result = module.grouppurchase.GroupPurchase.nondeposit_join(itemkey, userid)
        elif action == "leave":
            result = module.grouppurchase.GroupPurchase.join_leave(itemkey, userid)
        else:
            result = -5
            
        # for XML output
        path = os.path.join(os.path.dirname(__file__), '../templetes/gp_result.xml')
        self.response.out.write(template.render(path, 
                                                {"itemkey": itemkey, 
                                                 "action": action, 
                                                 "result": result, 
                                                 "userid": userid}))

def main():
    application = webapp.WSGIApplication([('/gp', MainHandler)],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()